<?php
require_once('lib/gymnasium.php');
?>

<h3>Statistiques</h3>

<p>Répartition des adhérents par :</p>

<div id="stats-tab-container">

<ul style="font-size: 12px;">
	<li><a href="#stats-tab-gym">Gymnase</a></li>
	<li><a href="#stats-tab-age">Age</a></li>
	<li><a href="#stats-tab-cat">Créneau horaire</a></li>
	<li><a href="#stats-tab-gender">Sexe</a></li>
	<li><a href="#stats-tab-city">Ville</a></li>
</ul>

<?php
$data = gym_count_members();
$members_per_gym = array();
while ($row = mysql_fetch_assoc($data)) {
	$members_per_gym[$row['name']] = $row['count_member'];
}
?>
<div id="stats-tab-gym">
	<h4>Répartition des adhérents par gymnase</h4>
	<table class="stats">
		<tr class="ui-widget-header">
			<th><strong>Gymnase :</strong></th>
			<?php
			foreach ($members_per_gym as $key => $value) {
				echo '<th>'.$key.'</th>';
			}
			?>
		</tr>
		<tr>
			<td><strong>Nombre d'adhérents :</strong></td>
			<?php
			foreach ($members_per_gym as $value) {
				echo '<td class="numeric">'.$value.'</td>';
			}
			?>
		</tr>
	</table>
	<div id="stats_gym" class="stats"></div>
</div>


<?php
$data = member_get_by_status('MEMBER');
$ages_count = array(
	8 => 0,
	12 => 0,
	15 => 0,
	18 => 0,
	999 => 0);
while ($row = mysql_fetch_assoc($data)) {
	$age = get_age($row['birth']);
	foreach ($ages_count as $key => $value) {
		if ($age < $key) {
			++$ages_count[$key];
			break;
		}
	}
}
?>
<div id="stats-tab-age">
	<h4>Répartition des adhérents par tranche d'age</h4>
	<table class="stats">
		<tr class="ui-widget-header">
			<th><strong>Age :</strong></th>
			<?php
			$last = 0;
			foreach ($ages_count as $key => $value) {
				if ($key < 999) {
					echo '<th>de '.$last.' à '.$key.'</th>';
				}
				else {
					echo '<th>'.$last.' et +</th>';
				}
				$last = $key;
			}
			?>
		</tr>
		<tr>
			<td><strong>Nombre d'adhérents :</strong></td>
			<?php
			foreach ($ages_count as $value) {
				echo '<td class="numeric">'.$value.'</td>';
			}
			?>
		</tr>
	</table>
	<div id="stats_age" class="stats"></div>
</div>


<?php
$data = member_stats_category();
$members_per_cat = array();
while ($row = mysql_fetch_assoc($data)) {
	$members_per_cat[$row['cat_name']] = $row['member_count'];
}
?>
<div id="stats-tab-cat">
	<h4>Répartition des adhérents par créneau horaire</h4>
	<table class="stats">
		<tr class="ui-widget-header">
			<th><strong>Créneau horaire :</strong></th>
			<?php
			foreach ($members_per_cat as $key => $value) {
				echo '<th>'.$key.'</th>';
			}
			?>
		</tr>
		<tr>
			<td><strong>Nombre d'adhérents :</strong></td>
			<?php
			foreach ($members_per_cat as $value) {
				echo '<td class="numeric">'.$value.'</td>';
			}
			?>
		</tr>
	</table>
	<div id="stats_cat" class="stats"></div>
</div>



<?php
$data = member_stats_gender();
$members_per_gender = array();
while ($row = mysql_fetch_assoc($data)) {
	$members_per_gender[$row['gender']] = $row['member_count'];
}
?>
<div id="stats-tab-gender">
	<h4>Répartition des adhérents par sexe</h4>
	<table class="stats">
		<tr class="ui-widget-header">
			<th><strong>Sexe :</strong></th>
			<?php
			foreach ($members_per_gender as $key => $value) {
				echo '<th>'.$key.' '.icon_tag($key == 'M' ? 'male' : ($key == 'F' ? 'female' : 'help')).'</th>';
			}
			?>
		</tr>
		<tr>
			<td><strong>Nombre d'adhérents :</strong></td>
			<?php
			foreach ($members_per_gender as $value) {
				echo '<td class="numeric">'.$value.'</td>';
			}
			?>
		</tr>
	</table>
	<div id="stats_gender" class="stats"></div>
</div>

<?php
$data = member_stats_city();
$members_per_city = array();
while ($row = mysql_fetch_assoc($data)) {
	$members_per_city[$row['city']] = $row['member_count'];
}
?>
<div id="stats-tab-city">
	<h4>Répartition des adhérents par ville</h4>
	<table class="stats">
		<tr class="ui-widget-header">
			<th><strong>Ville :</strong></th>
			<?php
			foreach ($members_per_city as $key => $value) {
				echo '<th>'.($key == '' ? icon_tag('help') : $key).'</th>';
			}
			?>
		</tr>
		<tr>
			<td><strong>Nombre d'adhérents :</strong></td>
			<?php
			foreach ($members_per_city as $value) {
				echo '<td class="numeric">'.$value.'</td>';
			}
			?>
		</tr>
	</table>
	<div id="stats_city" class="stats"></div>
</div>

</div> <!-- end stats-tab-container -->

<p>
	Les statistiques concernent uniquement les members dont l'inscription est validée
</p>

<script language="javascript" type="text/javascript">
var stats_ages = <?php echo '['.implode(', ', $ages_count).']' ?>;

$(function () {

	$("#stats-tab-container").tabs({
		show: function(event, ui) {
			url = $(ui.tab).attr('href');
			anchor = url.substr(url.lastIndexOf("#") + 1);
			switch (anchor) {
				case 'stats-tab-age':
					build_graph_age();
					break;
				case 'stats-tab-gym':
					build_graph_gym();
					break;
				case 'stats-tab-cat':
					build_graph_cat();
					break;
				case 'stats-tab-gender':
					build_graph_gender();
					break;
				case 'stats-tab-city':
					build_graph_city();
					break;
			}
		}
	});
});


function build_graph_age()
{
    $.plot($("#stats_age"), [
    	{
			data: [ [0, stats_ages[0]] ],
			bars: { show: true },
			label: "0 à 8"
		},
    	{
			data: [ [1, stats_ages[1]] ],
			bars: { show: true },
			label: "8 à 12"
		},
    	{
			data: [ [2, stats_ages[2]] ],
			bars: { show: true },
			label: "12 à 15"
		},
    	{
			data: [ [3, stats_ages[3]] ],
			bars: { show: true },
			label: "15 à 18"
		},
    	{
			data: [ [4, stats_ages[4]] ],
			bars: { show: true },
			label: "18+"
		}
	],
		{xaxis: { ticks: [], autoscaleMargin: 0.02 } }
	);
}

// graphe répartition des members par gymnase
function build_graph_gym()
{
	$.plot($("#stats_gym"), [
		<?php
		$i = 0;
		foreach ($members_per_gym as $key => $value) {
			echo '
			{
				data: [ ['.$i.', '.$value.'] ],
				bars: { show: true },
				label: "'.$key.'"
			},
			';
			++$i;
		}
		?>
	], {xaxis: { ticks: [], autoscaleMargin: 0.02 } }
	);
}

// graphe répartition des members par catégorie de créneau horaire
function build_graph_cat()
{
	$.plot($("#stats_cat"), [
		<?php
		$i = 0;
		foreach ($members_per_cat as $key => $value) {
			echo '
			{
				data: [ ['.$i.', '.$value.'] ],
				bars: { show: true },
				label: "'.$key.'"
			},
			';
			++$i;
		}
		?>
	], {xaxis: { ticks: [], autoscaleMargin: 0.02 } }
	);
}

// graphe répartition des members par sexe
function build_graph_gender()
{
	$.plot($("#stats_gender"), [
		<?php
		$i = 0;
		foreach ($members_per_gender as $key => $value) {
			echo '
			{
				data: [ ['.$i.', '.$value.'] ],
				bars: { show: true },
				label: "'.$key.'"
			},
			';
			++$i;
		}
		?>
	], {xaxis: { ticks: [], autoscaleMargin: 0.02 } }
	);
}

function build_graph_city()
{
	$.plot($("#stats_city"), [
		<?php
		$i = 0;
		foreach ($members_per_city as $key => $value) {
			echo '
			{
				data: [ ['.$i.', '.$value.'] ],
				bars: { show: true },
				label: "'.$key.'"
			},
			';
			++$i;
		}
		?>
	], {xaxis: { ticks: [], autoscaleMargin: 0.02 } }
	);
}

</script>
